//
//  SinglePickerView.m
//  DanaKreditLoan
//
//  Created by ZhengChengFeng on 2024/10/22.
//


#import "SinglePickerView.h"

#define kPickerRowHeight 50

#define kBottomFixHeight  (IS_IPhoneX_All ? kHeight_BottomSafe : 20)

#define kWholeViewHeight 370

@interface SinglePickerView ()<UIPickerViewDelegate,UIPickerViewDataSource>

@property (nonatomic) UIView *wholeView;

@property (nonatomic) UIPickerView *pickerView;;

@property (nonatomic) NSString *titleStr,*currentSelectTitleStr;

@end

@implementation SinglePickerView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
        self.backgroundColor = kShadowViewColor;
    }
    return self;
}

- (void)pickViewWithArray:(NSArray *)array andTitleStr:(NSString *)titleString{
    
    self.titleArray = [array mutableCopy];
        
    self.titleStr = titleString;
    
    [self createBottomView];
}

- (void)createBottomView{
    
    self.wholeView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, kWholeViewHeight)];
    [self addSubview:self.wholeView];
    self.wholeView.backgroundColor = [UIColor whiteColor];
    self.wholeView.layer.mask = [NSString UIBezierPathWithRect:_wholeView.bounds withCorners:UIRectCornerTopLeft|UIRectCornerTopRight withRadiu:CGSizeMake(14, 14)];
    
    UILabel *titleLab = [UILabel createLabel:[UIFont fontWithName:kFutura size:15] color:kBlackColor];
    titleLab.text = self.titleStr;
    titleLab.frame = CGRectMake(0, 30, kScreenWidth, 20);
    titleLab.textAlignment = 1;
    [self.wholeView addSubview:titleLab];
    
    UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth - 10 - 20, 10, 20, 20)];
    [cancelBtn setImage:[UIImage imageNamed:@"icon_cancel"] forState:UIControlStateNormal];
    [cancelBtn setImage:[UIImage imageNamed:@"icon_cancel"] forState:UIControlStateSelected];
    [cancelBtn setImage:[UIImage imageNamed:@"icon_cancel"] forState:UIControlStateHighlighted];
    [self.wholeView addSubview:cancelBtn];
    [cancelBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    cancelBtn.tag = 0;
    
    self.pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, titleLab.bottom  + 10, kScreenWidth, kWholeViewHeight- 60 - 48 - kBottomFixHeight - 10)];
    self.pickerView.showsSelectionIndicator=YES;
    self.pickerView.dataSource = self;
    self.pickerView.delegate = self;
    [self pickerView:self.pickerView didSelectRow:self.selectInteger inComponent:0];
    [self.wholeView addSubview:_pickerView];
    
    UIButton *sureButton = [[UIButton alloc]initWithFrame:CGRectMake(30, self.wholeView.height - kBottomFixHeight - 48, kScreenWidth - 60, 48)];
    sureButton.tag = 1;
    [sureButton setTitle:@"Confirming" forState:UIControlStateNormal];
    [sureButton setTitle:@"Confirming" forState:UIControlStateSelected];
    [sureButton setTitle:@"Confirming" forState:UIControlStateHighlighted];
    [sureButton setImage:[UIImage imageNamed:@"icon_login2"] forState:UIControlStateNormal];
    [sureButton setImage:[UIImage imageNamed:@"icon_login2"] forState:UIControlStateSelected];
    [sureButton setImage:[UIImage imageNamed:@"icon_login2"] forState:UIControlStateHighlighted];
    [sureButton setTitleColor:kWhiteColor forState:UIControlStateNormal];
    sureButton.titleLabel.font = [UIFont fontWithName:kFutura size:15];
    [sureButton addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.wholeView addSubview:sureButton];
    sureButton.backgroundColor = kButtonDefaultColor;
    sureButton.layer.cornerRadius = 24;
    sureButton.layer.masksToBounds = YES;
    [sureButton setImgRightTitltLeft:5];
    
    
}

- (void)clickBtn:(UIButton *)btn{

    if (btn.tag == 1){
        if (_block) {
            _block(self.currentSelectTitleStr);
        }
    }
    [self hiddenBottomView];
}

- (void)hiddenBottomView{
    kWeakSelf
    [UIView animateWithDuration:0.25f animations:^{
         weakSelf.wholeView.frame = CGRectMake(0, kScreenHeight, kScreenWidth, kWholeViewHeight);
     } completion:^(BOOL finished) {
         [self removeFromSuperview];
     }];
}

- (void)showBottomView{
    kWeakSelf
    [UIView animateWithDuration:0.25f animations:^{
        weakSelf.wholeView.frame = CGRectMake(0, kScreenHeight-kWholeViewHeight, kScreenWidth, kWholeViewHeight);
    } completion:^(BOOL finished) {}];
}


#pragma mark - UIPickerViewDelegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)componen{
    return self.titleArray.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return [self.titleArray objectAtIndex:row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    [pickerView selectRow:row inComponent:component animated:YES];
    self.currentSelectTitleStr = self.titleArray[row];
}


- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return kPickerRowHeight;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
   
    UILabel *norLabel = (UILabel*)view;
    if (!norLabel){
        norLabel = [UILabel createLabel:[UIFont fontWithName:kFutura size:15] color:[UIColor colorWithHexString:@"#6C6C6C"]];
        norLabel.textAlignment = 1;
    }
    norLabel.text = [self pickerView:pickerView titleForRow:row forComponent:component];
    

    UILabel *selLabel = (UILabel*)[pickerView viewForRow:row forComponent:0];
    if (selLabel) {
        selLabel.textColor = kButtonDefaultColor;
    }
    
    UILabel *nextSelLabel = (UILabel*)[pickerView viewForRow:row + 1 forComponent:0];
    if (nextSelLabel) {
        selLabel.textColor = kButtonDefaultColor;
    }
        
    return norLabel;
   
}



@end
